<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Arr;

class NotNullArrayValue implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    protected $nullValue;
    public function __construct(protected string $key, protected string $name,protected string $display)
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $bool = true;
        foreach ($value as $v) {
            if(empty(Arr::get($v, $this->key))) {
                $bool = false;
                $this->nullValue = Arr::get($v, $this->name);
                break;
            }
        }
        return $bool;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return sprintf('%s %s 不能为空', $this->nullValue,$this->display);
    }
}
